﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class Control_Upfile : System.Web.UI.UserControl
{
    public delegate void GetView(string info);
    public GetView reStr;
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        { }
    }
    private string _myValue;
    public string MyValue
    {
        set { _myValue = value; }
        get { return _myValue; }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (UpFile.HasFile)
        {
            SaveFile();
        }
        else
        {
            this.lblMsg.Text = "请选择需要上传的文件？";
        }
    }
    /// <summary>
    /// 保存上传的文件
    /// </summary>
    private void SaveFile()
    {
        //上传文件的绝对路径 如:C:\12.jpg
        string sFile = UpFile.PostedFile.FileName;

        //获取文件全名　例：12.jpg
        sFile = sFile.Substring(sFile.LastIndexOf("\\") + 1);

        //获取后缀名　例　.jpg
        sFile = sFile.Substring(sFile.LastIndexOf("."));
        Random Rnd = new Random();
        int strRnd = Rnd.Next(1, 99);
        //为了防止重名，获得日期为文件名  年月日时分秒毫秒
        string datatime = System.DateTime.Now.ToString("yyyyMMddHHmmssffff");

        //上传后文件的新名
        sFile = datatime + sFile;

        //AppDomain.CurrentDomain.BaseDirectory.ToString() 获取此项目的根目录
        //sPath 获取上传后的路径
        string sPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Images\\" + MyValue + "\\" + sFile;

        //上传文件
        UpFile.PostedFile.SaveAs(sPath);

        if (reStr !=null)
        {
            string fileUrl = "~/Images\\"+MyValue+"\\" + sFile;
            reStr(fileUrl);
        }
    }
}
         
       